package edu.gatech.mobile.scene.provider.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import edu.gatech.mobile.scene.model.GroundTruthImageInfo;
import edu.gatech.mobile.scene.model.ImageInfo;

/**
 * Helper for accessing data in the GroundTruth database table
 * @author Jing
 *
 */

public class GroundTruthDBImpl {
	private Context mContext;
	
	/**
	 * 
	 * @param context
	 */
	public GroundTruthDBImpl(Context context){
		mContext=context;
	}
	
	/**
	 * 
	 * @param imageInfo
	 */
	public void putImage(GroundTruthImageInfo imageInfo){
		ContentResolver resolver = mContext.getContentResolver();
		resolver.insert(GroundTruthInfoTable.CONTENT_URI, getQueryValues(imageInfo));
	}
	
	/**
	 * Getting db cursor points to all of the ground truth images
	 * @return the cursor
	 */
	public Cursor getAllImagesCursor(){
		ContentResolver resolver = mContext.getContentResolver();
		Cursor cursor =resolver.query(GroundTruthInfoTable.CONTENT_URI, null, null, null, null);
		return cursor;
	}
	
	/**
	 * retrieve all of the ground truth images in the database.
	 * @return a list of all the groundtruth images.
	 */
	public List<GroundTruthImageInfo> getAllGroundTruthImages(){
		List<GroundTruthImageInfo> items = new ArrayList<GroundTruthImageInfo>();
		ContentResolver resolver = mContext.getContentResolver();
		Cursor cursor =resolver.query(GroundTruthInfoTable.CONTENT_URI, null, null, null, null);
		
		while (cursor != null && cursor.getCount() > 0 && !cursor.isLast()){
			GroundTruthImageInfo item = fillFromCursor(cursor);
			items.add(item);
		}
		safelyCloseCursor(cursor);
		return items;
	}
	
	/**
	 *  remove the given image from the database
	 * @param imageInfo image to be removed.
	 */
	public void removeGroundTruthImage(ImageInfo imageInfo){
		ContentResolver resolver = mContext.getContentResolver();
		final String where= GroundTruthInfoTable.ORIGINAL_IMAGE_NAME + "='"+imageInfo.getFileName()+"'";
		resolver.delete(GroundTruthInfoTable.CONTENT_URI, where, null);
	}
	
	
	/**
	 * Get the ground truth image for the given original image.
	 * @param origImagename 
	 * @return the ground truth labeled image associated with the original image.
	 */
	public GroundTruthImageInfo getGroundTruthforImage(String origImagename){
		ContentResolver resolver = mContext.getContentResolver();
		String where = GroundTruthInfoTable.ORIGINAL_IMAGE_NAME + "='"+origImagename+"'";
		Cursor cursor =resolver.query(GroundTruthInfoTable.CONTENT_URI, null, where, null, null);
		if (cursor != null && cursor.getCount() > 0 && !cursor.isLast()){
			GroundTruthImageInfo item = fillFromCursor(cursor);
			safelyCloseCursor(cursor);
			return item;
		}
		else return null;
	}
	
	/**
	 * Update the given groundTruthImage in the database.
	 * @param imageInfo imageInfo to be udpated.
	 */
	public void updateImage(GroundTruthImageInfo imageInfo){
		String where = GroundTruthInfoTable.ORIGINAL_IMAGE_NAME + "='"+imageInfo.getOriginalImageName() +"'";
		ContentValues fields = getQueryValues(imageInfo);
		ContentResolver resolver = mContext.getContentResolver();
		resolver.update(GroundTruthInfoTable.CONTENT_URI, fields, where, null);
	}
	
	/**
	 * construct a table of database column name and corresponding values
	 * @param imageInfo
	 * @return the table of column name the value.
	 */
	public ContentValues getQueryValues(GroundTruthImageInfo imageInfo) {
		long time;
		if(imageInfo.getTimeCreated() !=null){
			time=imageInfo.getTimeCreated();
		}
		else{
			time = System.currentTimeMillis();
		}
		ContentValues values = new ContentValues();
		values.put(GroundTruthInfoTable.TIME_POSTED, time);
		values.put(GroundTruthInfoTable.ORIGINAL_IMAGE_NAME, imageInfo.getOriginalImageName());
		values.put(GroundTruthInfoTable.TRUTH_LABEL_NAME, imageInfo.getTruthLabelName());
		return values;
		
	}
	
	/**
	 * Create the groundTruthImage using the values in the cursor.
	 * @param cursor cursor points at a row in the table
	 * @return the object with values in the row.
	 */
	public GroundTruthImageInfo fillFromCursor(Cursor cursor) {
		cursor.moveToNext();
		GroundTruthImageInfo imageInfo = new GroundTruthImageInfo();
		imageInfo.setOriginalImageName(cursor.getString(cursor.getColumnIndex(GroundTruthInfoTable.ORIGINAL_IMAGE_NAME)));
		imageInfo.setTruthLabelName(cursor.getString(cursor.getColumnIndex(GroundTruthInfoTable.TRUTH_LABEL_NAME)));
		imageInfo.setTimeCreated(cursor.getLong(cursor.getColumnIndex(GroundTruthInfoTable.TIME_POSTED)));
		return imageInfo;
	}
	
	
	private void safelyCloseCursor(Cursor cur) {
		if(cur != null && cur.isClosed() == false)
			cur.close();
	}
	
	
}
